package cn.nio.文件复制;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author Mxb
 * @version 1.0
 * @date 2020/7/26 20:57
 */
public class CopyFile {
    private ByteBuffer buffer;

    public static void main(String[] args) {
        try {
            FileInputStream fis = new FileInputStream("basic.txt");
            FileOutputStream fos = new FileOutputStream("basic233.txt");

            FileChannel channel = fos.getChannel();
            FileChannel channel1 = fis.getChannel();

            ByteBuffer buffer = ByteBuffer.allocate(20);
            // 将数据从文件中读到 缓冲区
/*            channel1.read(buffer);
            System.out.println(new String(buffer.array()));

            buffer.flip();
             将缓冲区数据 写入到文件中
            channel.write(buffer);
            buffer.flip();*/
            /**
             * 一个方法搞定
             */
            channel.transferFrom(channel1,0,channel1.size());
            //channel.transferTo()

            fos.close();
            fis.close();



        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
